package com.duoduo.springboot.shiro.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.duoduo.springboot.shiro.model.User;

@Repository
public class UserDao {

	private static Map<Object, User> userDatas = new HashMap<Object, User>(0);

	private Map<Object, User> listUsers() {
		if (CollectionUtils.isEmpty(userDatas)) {
			User user = new User(1, "admin", "888888");
			userDatas.put(user.getId(), user);
			userDatas.put(user.getAccount(), user);

			user = new User(2, "cgs", "888888");
			userDatas.put(user.getId(), user);
			userDatas.put(user.getAccount(), user);
		}
		return userDatas;
	}

	public List<User> listAll() {
		List<User> result = new ArrayList<User>(0);
		Map<Object, User> map = listUsers();
		if (!CollectionUtils.isEmpty(map)) {
			for (Entry<Object, User> entry : map.entrySet()) {
				User user = entry.getValue();
				if (!result.contains(user)) {
					result.add(user);
				}
			}
		}
		return result;
	}

	public User getById(Integer userId) {
		return listUsers().get(userId);
	}

	public User getByAccount(String account) {
		return listUsers().get(account);
	}
}
